/**
 * parser - jQuery xui
 *
 * Licensed under the GPL: http://www.gnu.org/licenses/gpl.txt
 *
 * Copyright 2013 xjb [ beymy.en@gmail.com ]
 *
 */
(function($) {
    $.parser = {
        /**
         * 解析data-options属性
         *
         * 调用例子:
         * $.parser.parseOptions(target);
         * $.parser.parseOptions(target, ['id','title','width',{fit:'boolean',border:'boolean'},{min:'number'}]);
         */
        parseOptions: function(target, properties) {
            var t = $(target);
            var options = {};

            var s = $.trim(t.attr('data-options'));
            if (s) {
                if (s.substring(0, 1) != '{') {
                    s = '{' + s + '}';
                }
                options = (new Function('return ' + s))();
            }

            if (properties) {
                var opts = {};
                for (var i = 0; i < properties.length; i++) {
                    var pp = properties[i];
                    if (typeof pp == 'string') {
                        if (pp == 'width' || pp == 'height' || pp == 'left' || pp == 'top') {
                            opts[pp] = parseInt(target.style[pp]) || undefined;
                        } else {
                            opts[pp] = t.attr(pp);
                        }
                    } else {
                        for (var name in pp) {
                            var type = pp[name];
                            if (type == 'boolean') {
                                opts[name] = t.attr(name) ? (t.attr(name) == 'true') : undefined;
                            } else if (type == 'number') {
                                opts[name] = t.attr(name) == '0' ? 0 : parseFloat(t.attr(name)) || undefined;
                            }
                        }
                    }
                }
                $.extend(options, opts);
            }
            return options;
        }
    };

    /**
     * 设置box model宽度
     */
    $.fn._outerWidth = function(width) {
        if (width == undefined) {
            return this.outerWidth() || 0;
        }
        return this.each(function() {
            $(this).width(width - ($(this).outerWidth() - $(this).width()));
        });
    };

    /**
     * 设置box model高度
     */
    $.fn._outerHeight = function(height) {
        if (height == undefined) {
            return this.outerHeight() || 0;
        }
        return this.each(function() {
            $(this).height(height - ($(this).outerHeight() - $(this).height()));
        });
    };

    $.fn._fit = function(fit) {
        fit = (fit == undefined) ? true : fit;
        var t = this[0];
        var p = (t.tagName == 'BODY' ? t : this.parent()[0]);
        if (fit) {
            $(p).addClass('panel-noscroll');
            if (p.tagName == 'BODY') {
                $('html').addClass('panel-fit');
            }
        } else {
            $(p).removeClass('panel-noscroll');
            if (p.tagName == 'BODY') {
                $('html').removeClass('panel-fit');
            }
        }
        return {
            width: $(p).width(),
            height: $(p).height()
        }
    }
    
	/**
	 * 判断属性集中的任一属性是否存在
	 */
    $.inObject = function(array, object) {
    	for(var i = 0; i < array.length; i ++) {
    		if(object[array[i]] != undefined) {
    			return true;
    		}
    	}
    	return false;
    }

    //resize事件
    $(window).bind('resize', function() {
        $('body.layout,div.layout,div.window').triggerHandler('_resize');
    });

})(jQuery);
